home *** CD-ROM | disk | FTP | other *** search
Text File | 1995-06-15 | 4.1 KB | 131 lines | [TEXT/PJMM] |
- {MicroAnimationDemo by Ingemar Ragnemalm}
-
- {Was OffscreenToys too elaborate for you? I know, it keeps some extra information to avoid}
- {redrawing the whole window, and the collision handling is pretty fancy. This is stipped down}
- {to the limit!}
-
- {This was really intended for the Mac game programming book, as a first intro to offscren}
- {animation, but they found someone else to make that chapter, so what the heck, then I'll}
- {let it be a free little demo (and won't bother making a C version).}
-
- {So, if it is THIS simple to make sprite animation, what's the point with SAT or even}
- {OffscreenToys? I'll tell you: Speed, flexibility, compatibility, expandability.}
-
- {PlotCIcon is not the fastest routine around. Actually, it is one of the slower. Check out}
- {OffScreenToysBoost to learn how to speed it up (replacing PlotCIcon with CopyBits).}
- {Also, it can be speeded up by not erasing the entire offscreen and by not copying all of it}
- {every frame. However, more speed takes more code.}
-
- program MicroAnimationDemo;
- uses
- {$IFC UNDEFINED THINK_PASCAL}
- Types, QuickDraw, Fonts, Events, Packages, Menus, Dialogs, Windows,{}
- OSUtils, ToolUtils, Resources, Icons,
- {$ENDC}
- QDOffScreen;
- const
- kMaxObject = 5;
- var
- cicn: array[0..kMaxObject] of CIconHandle;
- pos: array[0..kMaxObject] of Rect;
- speed: array[0..kMaxObject] of Point;
- wind: WindowPtr;
- offScreen: GrafPtr;
- backPat: PixPatHandle;
- i: Integer;
- startTicks: Longint;
-
- (* Standard inits *)
- procedure InitToolbox;
- begin
- {$IFC UNDEFINED THINK_PASCAL}
- InitGraf(@qd.thePort);
- InitFonts;
- FlushEvents(everyEvent, 0);
- InitWindows;
- InitMenus;
- TEInit;
- InitDialogs(nil);
- InitCursor;
- {$ENDC}
- end; (*InitToolbox*)
-
- begin
- InitToolbox;
-
- {Is Color QD and 32-bit QD around?}
- if NGetTrapAddress($A89F, ToolTrap) = NGetTrapAddress($AA1E, ToolTrap) then {Color QD?}
- halt;
- if NGetTrapAddress($A89F, ToolTrap) = NGetTrapAddress($AB1D, ToolTrap) then {32-bit QD?}
- halt;
-
- {Seed the random number generator}
- {$IFC UNDEFINED THINK_PASCAL}
- qd.randSeed := TickCount;
- {$ELSEC}
- randSeed := TickCount;
- {$ENDC}
-
- {Create window from resource}
- wind := GetNewCWindow(128, nil, WindowPtr(-1));
- SetPort(wind);
-
- {Make GWorld with default depth and CLUT}
- {$IFC UNDEFINED THINK_PASCAL}
- if noErr <> NewGWorld(GWorldPtr(offScreen), 0, wind^.portRect, nil, nil, pixelsLocked) then
- {$ELSEC}
- if noErr <> NewGWorld(GWorldPtr(offScreen), 0, wind^.portRect, nil, nil, [pixelsLocked]) then
- {$ENDC}
- halt;
- {We lock the offscreen pixmap so we can CopyBits and PlotCIcon to it.}
- if LockPixels(CGrafPtr(offScreen)^.portPixMap) then
- ;
-
- SetGWorld(GWorldPtr(offScreen), nil);
-
- {Get a pattern and set it to the background pattern for offScreen}
- backPat := GetPixPat(128);
- BackPixPat(backPat);
-
- {Set up all objects, loading the 'cicn' resource, and setting its position and speed}
- for i := 0 to kMaxObject do
- begin
- cicn[i] := GetCIcon(128 + i);
- pos[i] := cicn[i]^^.iconMask.bounds;
- OffsetRect(pos[i], abs(Random) mod wind^.portRect.right, abs(Random) mod wind^.portRect.bottom);
- speed[i].h := Random mod 8;
- speed[i].v := Random mod 8;
- end;
-
- {*** The animation loop: ***}
-
- while not Button do
- begin
- startTicks := TickCount;
- {Erase the offscreen, resetting it to the background image/pattern}
- SetGWorld(GWorldPtr(offScreen), nil);
- EraseRect(wind^.portRect);
- {For all objects, move, draw off-screen and do border checks}
- for i := 0 to kMaxObject do
- begin
- OffSetRect(pos[i], speed[i].h, speed[i].v);
- PlotCIcon(pos[i], cicn[i]);
- if pos[i].top < 0 then
- speed[i].v := abs(speed[i].v);
- if pos[i].left < 0 then
- speed[i].h := abs(speed[i].h);
- if pos[i].bottom > wind^.portRect.bottom then
- speed[i].v := -abs(speed[i].v);
- if pos[i].right > wind^.portRect.right then
- speed[i].h := -abs(speed[i].h);
- end;
- {Copy from offscreen to screen}
- SetGWorld(GWorldPtr(wind), GetMainDevice);
- CopyBits(offScreen^.portBits, wind^.portBits, wind^.portRect, wind^.portRect, srcCopy, nil);
- {Wait until at least one tick has passed}
- while TickCount = startTicks do
- ;
- end;
- {Set back the device before we quit}
- SetGWorld(GWorldPtr(wind), GetMainDevice);
- end. {MicroAnimationDemo}